package com.luxsoft.siipap.em.replica;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.MessageFormat;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.util.ClassUtils;

import com.luxsoft.siipap.em.replica.model.Registro;
import com.luxsoft.siipap.em.service.EMServiceLocator;
import com.luxsoft.siipap.service.ServiceLocator2;

public class ReplicaDbUtils {
	
	
	
	
	
	public static void printCamposDeDBF(String tabla){
		JdbcTemplate template=EMServiceLocator
			.instance().getSiipapTemplateFactory()
			.getCurrentJdbcTemplate();
		
		template.query("select * from "+tabla+" where 1=2", new ResultSetExtractor(){

			public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
				ResultSetMetaData meta=rs.getMetaData();
				int cols=meta.getColumnCount();
				for(int i=1;i<=cols;i++){
					String col=meta.getColumnName(i);
					String msg=MessageFormat.format("\"{0}\",", col);
					System.out.print(msg);
				}
				return null;
			}
			
		});
	}
	
	public static void printCamposDeDBFParaXML(String tabla){
		JdbcTemplate template=EMServiceLocator
			.instance().getSiipapTemplateFactory()
			.getCurrentJdbcTemplate();
		
		template.query("select * from "+tabla+" where 1=2", new ResultSetExtractor(){

			public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
				ResultSetMetaData meta=rs.getMetaData();
				int cols=meta.getColumnCount();
				for(int i=1;i<=cols;i++){
					String col=meta.getColumnName(i);
					String msg=MessageFormat.format("<entry key=\"\" 			value=\"{0}\" />", col);
					System.out.println(msg);
				}
				return null;
			}
			
		});
	}
	
	public static void printExtractCamposAsJavaTypes(String tabla){
		JdbcTemplate template=EMServiceLocator
			.instance().getSiipapTemplateFactory()
			.getCurrentJdbcTemplate();
		
		template.query("select * from "+tabla+" where 1=2", new ResultSetExtractor(){

			public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
				ResultSetMetaData meta=rs.getMetaData();
				int cols=meta.getColumnCount();
				for(int i=1;i<=cols;i++){
					String col=meta.getColumnName(i);
					String type=meta.getColumnClassName(i);
					try {
						Class clazz=Class.forName(type);
						if(Number.class.isAssignableFrom(clazz))
							clazz=Number.class;
						String cn=ClassUtils.getShortName(clazz);
						String method="getString";
						if(clazz==java.sql.Date.class)
							method="getDate";
						else if(clazz==Number.class)
							method="getNumber";
						String msg=MessageFormat.format("{0} {1}=r.{2}(\"{3}\");", cn,col,method,col);
						System.out.println(msg);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
				return null;
			}
			
		});
	}
	
	/**
	 * Imprime una lista de  los campos de una tabla con
	 * su java type nombre de variable y forma de extraer del ResultSet
	 * 
	 * Eje:
	 *	 	String CLICLAVE=rs.getString("CLICLAVE");
	 *		String CLICLAVORI=rs.getString("CLICLAVORI");
	 *
	 * Util para facilitar la creacion de un bean que emule los campos
	 * de una tabla
	 * 
	 * Nota Tambien funciona para extraer informacion de una instancia 
	 * de {@link Registro}
	 * 
	 * @param tabla
	 */
	public static void extractCamposAsJavaTypesFromResultSet(final JdbcTemplate template,String tabla){
		
		
		template.query("select * from "+tabla+" where 1=2", new ResultSetExtractor(){

			public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
				ResultSetMetaData meta=rs.getMetaData();
				int cols=meta.getColumnCount();
				for(int i=1;i<=cols;i++){
					String col=meta.getColumnName(i);
					String type=meta.getColumnClassName(i);
					try {
						Class clazz=Class.forName(type);
						
						String method="getString";
						if(clazz==java.sql.Date.class)
							method="getDate";
						else if(clazz==BigDecimal.class)
							method="getBigDecimal";
						else if(clazz==Double.class)
							method="getDouble";
						String convert="";
						if(Number.class.isAssignableFrom(clazz)){
							convert="(Number)";
							clazz=Number.class;
						}
						String cn=ClassUtils.getShortName(clazz);
						String msg=MessageFormat.format("{0} {1}={2}rs.{3}(\"{4}\");", cn,col,convert,method,col);
						System.out.println(msg);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
				return null;
			}
			
		});
	}
	
	/**
	 * Imprimie en el system.out los campos de la tabla indicada desde siipap - dbf
	 * @param tabla
	 */
	public static void extractCamposAsJavaTypesFromResultSet(String tabla){
		extractCamposAsJavaTypesFromResultSet(EMServiceLocator
				.instance().getSiipapTemplateFactory()
				.getCurrentJdbcTemplate(), tabla);
	}
	
	
	
	/**
	private static String parse(int type){
		switch (type) {
		case Types.:
			
			break;

		default:
			break;
		}
	}
	*/
	
	public static void main(String[] args) {
		//extractCamposAsJavaTypesFromResultSet("ALMACE01");
		//extractCamposAsJavaTypesFromResultSet("SW_PAGOS");
		extractCamposAsJavaTypesFromResultSet(ServiceLocator2.getAnalisisJdbcTemplate(), "SW_PAGOS");
		//printCamposDeDBF("ALMACE01");
	}

}
